package com.example.tcccoordinatorexample.controller;


import com.example.tcccoordinatorexample.dto.OrderRequest;
import com.example.tcccoordinatorexample.service.TccOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@RestController
@RequestMapping("tcc")
public class TccController {
    @Autowired
    private TccOrderService orderService;

    @RequestMapping(value = "/newOrderWithTcc", method = {RequestMethod.POST, RequestMethod.GET})
    public String newOrderWithTcc() {
        String txId = UUID.randomUUID().toString();
//        OrderRequest orderRequest = OrderRequest.builder()
//                .userId("caibosi")
//                .productCode("spring-cloud-in-action")
//                .quantity(10)
//                .build();
        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setUserId("caibosi");
        orderRequest.setProductCode("spring-cloud-in-action");
        orderRequest.setQuantity(10);
        orderService.newOrderWithTcc(orderRequest, txId);
        return "success";
    }

    @RequestMapping(value = "/newOrderWithTccRetry", method = {RequestMethod.POST, RequestMethod.GET})
    public String newOrderWithTccRetry() {
        String txId = "972c86d6-6bda-4859-9128-de10f4bf9634";
//        OrderRequest orderRequest = OrderRequest.builder()
//                .userId("caibosi")
//                .productCode("spring-cloud-in-action")
//                .quantity(10)
//                .build();
        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setUserId("caibosi");
        orderRequest.setProductCode("spring-cloud-in-action");
        orderRequest.setQuantity(10);
        orderService.newOrderWithTcc(orderRequest, txId);
        return "success";
    }

    @RequestMapping(value = "/newOrderWithTccRollback", method = {RequestMethod.POST, RequestMethod.GET})
    public String newOrderWithTccRollback() {
        String txId = UUID.randomUUID().toString();
//        OrderRequest orderRequest = OrderRequest.builder()
//                .userId("caibosi")
//                .productCode("spring-cloud-in-action")
//                .quantity(999)
//                .build();
        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setUserId("caibosi");
        orderRequest.setProductCode("spring-cloud-in-action");
        orderRequest.setQuantity(999);
        orderService.newOrderWithTcc(orderRequest, txId);
        return "success";
    }
}